package hx.util;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HX<titanrain@163.com>
 */
public class SqlUtil {

    public static String joinInValues(Object[] inValues,String defaultWhenEmpty){
        String ret = "";
        if(inValues==null||inValues.length==0){
            ret = defaultWhenEmpty;
        } else {
            ret = StringUtils.join(inValues,',');
        }
        return ret;
    }

    public static String joinQuestionMark(int size) {
        StringBuffer ret = new StringBuffer("");
        if (size <= 0) {
            return "";
        }
        for (int i = 0; i < size; i++) {
            ret.append("?,");
        }
        ret.deleteCharAt(ret.length() - 1);
        return ret.toString();
    }

    public static List<Integer> strIdToIntId(String[] strIds) {
        List<Integer> ids = new ArrayList<Integer>();
        if (strIds == null || strIds.length == 0) {
            return ids;
        }
        for (String id : strIds) {
            ids.add(Integer.parseInt(id));
        }
        return ids;
    }

    public static List<Integer> strIdToIntId(String strIds) {
        String[] idArr = strIds.split(",");
        return strIdToIntId(idArr);
    }

    public static List<Long> strIdToLongId(String[] strIds) {
        List<Long> ids = new ArrayList<Long>();
        if (strIds == null || strIds.length == 0) {
            return ids;
        }
        for (String id : strIds) {
            ids.add(Long.parseLong(id));
        }
        return ids;
    }

    public static List<Long> strIdToLongId(String strIds) {
        String[] idArr = strIds.split(",");
        return strIdToLongId(idArr);
    }
}
